//
//  MyConcernNewsDetailViewController.m
//  homeland
//
//  Created by SunShine on 13-10-20.
//  Copyright (c) 2013年 xzysun. All rights reserved.
//

#import "MyConcernNewsDetailViewController.h"
#import "UIImageView+WebCache.h"
#import "ImportantNewsItem.h"

@interface MyConcernNewsDetailViewController ()

@end

@implementation MyConcernNewsDetailViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.title = @"详情";
    [self configView];
}

-(void)configView
{
    ImportantNewsItem *item = [_config objectForKey:@"dataItem"];
    //根据内容调整高度
    CGFloat titleHeight = [self caculateHeightForString:item.title AndFontSize:20.0f AndWitdh:300.0f];
    CGFloat contentHeight = [self caculateHeightForString:item.detail AndFontSize:14.0f AndWitdh:300.0f];
    NSLog(@"计算出标题高度%f正文高度%f", titleHeight, contentHeight);
    self.titleTextView.text = item.title;
    self.titleTextView.frame = CGRectMake(10.0, 10.0, 300.0, titleHeight);
    self.imageView.frame = CGRectMake(10.0, CGRectGetMaxY(self.titleTextView.frame) + 10.0, 300.0, 220.0);
    self.contentTextView.text = item.detail;
    self.contentTextView.frame = CGRectMake(10.0, CGRectGetMaxY(self.imageView.frame) + 10.0, 300.0, contentHeight);
    CGFloat totalHeight = MAX(CGRectGetMaxY(self.contentTextView.frame) + 10.0, CGRectGetHeight(self.scrollView.frame));
    self.contentPanel.frame = CGRectMake(0.0, 0.0, 320.0, totalHeight);
    [self.scrollView addSubview:self.contentPanel];
    self.scrollView.contentSize = self.contentPanel.frame.size;
    //图片
    NSString *pic = [NSString stringWithFormat:@"http://%@%@", NEWS_PIC_BASE_URL, item.picture];
    __block UIActivityIndicatorView *activityIndicator;
    __weak UIImageView *weakImageView = self.imageView;
    [self.imageView setImageWithURL:[NSURL URLWithString:pic] placeholderImage:[UIImage imageNamed:@"placeholder.png"] options:SDWebImageProgressiveDownload progress:^(NSUInteger receivedSize, long long expectedSize)
     {
         if (!activityIndicator)
         {
             [weakImageView addSubview:activityIndicator = [UIActivityIndicatorView.alloc initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray]];
             activityIndicator.center = weakImageView.center;
             [activityIndicator startAnimating];
         }
     } completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType)
     {
         [activityIndicator removeFromSuperview];
         activityIndicator = nil;
     }];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
@end
